package org.szgb.console.sys.dict.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysDictChildren extends BeanEntity<SysDictChildren> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysDict dict;
    private String label;
    private String value;

    public SysDictChildren() {
    }

    public SysDictChildren(String id) {
        this.setId(id);
    }

    public SysDictChildren(SysDictChildren.SysDictChildrenFindDto sysDictChildrenFindDto) {
        this.setId(sysDictChildrenFindDto.getId());
        this.setDict(new SysDict(sysDictChildrenFindDto.getDictId()));
        this.setPageDto(sysDictChildrenFindDto.getPageDto());
    }

    public SysDictChildren(SysDictChildren.SysDictChildrenSaveDto sysDictChildrenSaveDto) {
        this.setId(sysDictChildrenSaveDto.getId());
        if (sysDictChildrenSaveDto.getDictId() != null && !sysDictChildrenSaveDto.getDictId().isEmpty()) {
            this.setDict(new SysDict(sysDictChildrenSaveDto.getDictId()));
        }
        this.setLabel(sysDictChildrenSaveDto.getLabel());
        this.setValue(sysDictChildrenSaveDto.getValue());
    }

    public SysDictChildren.SysDictChildrenView toSysDictChildrenView() {
        return new SysDictChildren.SysDictChildrenView(this);
    }

    public SysDictChildren.SysDictChildrenTreeView toSysDictChildrenTreeView() {
        return new SysDictChildren.SysDictChildrenTreeView(this);
    }

    @Data
    @Schema(title = "字典子项信息", description = "字典子项信息")
    public static class SysDictChildrenView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "字典id")
        private String dictId;
        @Schema(description = "字典名称")
        private String dictName;
        @Schema(description = "标签")
        private String label;
        @Schema(description = "值")
        private String value;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysDictChildrenView(SysDictChildren sysDictChildren) {
            this.id = sysDictChildren.getId();
            this.dictId = sysDictChildren.getDict().getId();
            this.dictName = sysDictChildren.getDict().getName();
            this.label = sysDictChildren.getLabel();
            this.value = sysDictChildren.getValue();
            this.createdAt = sysDictChildren.getCreatedAt();
            this.updatedAt = sysDictChildren.getUpdatedAt();
            if (sysDictChildren.getCreatedBy() != null) {
                this.createdById = sysDictChildren.getCreatedBy().getId();
                this.createdByName = sysDictChildren.getCreatedBy().getName();
            }
            if (sysDictChildren.getUpdatedBy() != null) {
                this.updatedById = sysDictChildren.getUpdatedBy().getId();
                this.updatedByName = sysDictChildren.getUpdatedBy().getName();
            }
        }

        public static List<SysDictChildren.SysDictChildrenView> getViewListFromList(List<SysDictChildren> sysDictChildrenList) {
            return sysDictChildrenList.stream().map(SysDictChildren.SysDictChildrenView::new).toList();
        }
    }

    @Data
    @Schema(title = "字典子项树信息", description = "字典子项树信息")
    public static class SysDictChildrenTreeView {
        @Schema(description = "标签")
        private String label;
        @Schema(description = "值")
        private String value;

        public SysDictChildrenTreeView(SysDictChildren sysDictChildren) {
            this.label = sysDictChildren.getLabel();
            this.value = sysDictChildren.getValue();
        }
    }

    @Data
    @Schema(title = "字典子项查询表单", description = "字典子项查询参数")
    public static class SysDictChildrenFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "字典id")
        private String dictId;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysDictChildren toSysDictChildren() {
            return new SysDictChildren(this);
        }
    }

    @Data
    @Schema(title = "字典子项保存表单", description = "字典子项保存参数")
    public static class SysDictChildrenSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "字典id")
        private String dictId;
        @Schema(description = "标签")
        private String label;
        @Schema(description = "值")
        private String value;

        public SysDictChildren toSysDictChildren() {
            return new SysDictChildren(this);
        }
    }
}
